﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BAL;
using Channelvn.Cached.Common;
using System.Configuration;

namespace ProjectQuaDep.Ajax
{
    public partial class AjaxSearchListXemBinhChon : System.Web.UI.Page
    {
        private int Top_Cont = 4;
        private int Width_Slide = 491;
        private int Height_Slide = 307;
        private int CurrentRound = 0;
        private int awardId = 0, pageIndex = 0, pageType = 0;
        private string url = string.Empty, keySearch = string.Empty;
        private string startDate, endDate;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                awardId = Lib.QueryString.AwardsID;

                pageIndex = Lib.QueryString.PageIndex;
                url = Lib.QueryString.pageurl;
                pageType = 2;// Lib.QueryString.PageType;
                keySearch = BAL.Utils.UnicodeToKoDau(Lib.QueryString.KeySearch).ToLower().Replace(" ", ""); 
                awardId = Lib.QueryString.AwardsID;
                if (awardId > 0)
                {
                    startDate = Utils.VongThiStart(awardId);
                    endDate = Utils.VongThiEnd(awardId);
                }
                DataTable dt = null;
                if (awardId > 0)
                    dt = BussinessHelpers.SearchXemBinhChon(keySearch, startDate, endDate, Width_Slide, Height_Slide, pageIndex, Top_Cont, pageType);
                else
                    dt = BussinessHelpers.SearchXemBinhChon_NoDateTime(keySearch, Width_Slide, Height_Slide, pageIndex, Top_Cont, pageType);
                if (Utils.TableIsNotNullOrEmpty(dt))
                {
                    rptData.DataSource = dt;
                    rptData.DataBind();
                }
                else
                {
                    divContent.Visible = false;
                    ltrMessage.Text = "Không tìm thấy hình ảnh nào theo từ khóa của bạn!";
                }
                if (awardId > 0)
                    this.CustomPaging1.SumPage = BussinessHelpers.SearchXemBinhChonPageNum(keySearch, startDate, endDate, Top_Cont);
                else
                    this.CustomPaging1.SumPage = BussinessHelpers.SearchXemBinhChon_NoDateTime_PageNum(keySearch, Top_Cont);
                this.CustomPaging1.BuildLink = "<a class=\"{0}\" href=\"javascript:void(0)\" onclick=\"ajaxSearchXemBinhChon(" + awardId + ",'" + keySearch + "',{1})\">{2}</a>&nbsp;";
                this.CustomPaging1.Padding(pageIndex);
            }
        }

        protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Literal ltrBinhChon = (Literal)e.Item.FindControl("ltrBinhChon");
                int ImageId = Convert.ToInt32(((DataRowView)e.Item.DataItem).Row[Const.Images.ImageId].ToString());
                int LikeWeb = 0;
                if (!string.IsNullOrEmpty(((DataRowView)e.Item.DataItem).Row[Const.Images.LikeWeb].ToString()))
                    LikeWeb = Convert.ToInt32(((DataRowView)e.Item.DataItem).Row[Const.Images.LikeWeb].ToString());
                string Url = ((DataRowView)e.Item.DataItem).Row[Const.Images.Url].ToString();

                if (LikeWeb > 0)
                    ltrBinhChon.Text = LikeWeb.ToString();
                //ltrScritp.Text += "<script type=\"text/javascript\">getFacebookCommentCount('" + ConfigurationManager.AppSettings["Domain"] + Url + "', function (count) {if(count> 0)  $('.count_" + ImageId + " div.innertext').html(count + ' bình luận'); });</script>";
                string strQuery = "https://graph.facebook.com/fql?q=SELECT%20url,%20normalized_url,%20share_count,%20like_count,%20comment_count,%20total_count,commentsbox_count,%20comments_fbid,%20click_count%20FROM%20link_stat%20WHERE%20url=%27" + ConfigurationManager.AppSettings["Domain"] + Url + "%27";
                ltrScritp.Text += "<script type=\"text/javascript\">getFacebookCommentCount('" + strQuery + "', function (share,comment) {if(share> 0)  $('.countcs_" + ImageId + " div.innertext').html(share); if(comment> 0)  $('.count_" + ImageId + " div.innertext').html(comment); });</script>";
            }
        }
    }
}